import 'package:calendar_date_picker2/calendar_date_picker2.dart';
import 'package:flutter/material.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';

enum PickerType { single, range }

class CommonDate {
  static Future<List<DateTime?>?> showPicker({
    required BuildContext context,
    PickerType pickerType = PickerType.single,
    List<DateTime>? value,
    DateTime? firstDate,
    DateTime? lastDate,
    DateTime? currentDate,
  }) {
    return showCalendarDatePicker2Dialog(
      context: context,
      config: _getConfig(pickerType,
          firstDate: firstDate, currentDate: currentDate, lastDate: lastDate),
      dialogSize: Size(SizeUtils.screenW() * 0.9, 370.px),
      borderRadius: BorderRadius.circular(15.px),
      value: value ?? [DateTime.now()],
      dialogBackgroundColor: WColors.white,
    );
  }

  static _getConfig(
    PickerType pickerType, {
    DateTime? firstDate,
    DateTime? lastDate,
    DateTime? currentDate,
  }) {
    return CalendarDatePicker2WithActionButtonsConfig(
      calendarViewScrollPhysics: const NeverScrollableScrollPhysics(),
      dayTextStyle:
          const TextStyle(color: Colors.black, fontWeight: FontWeight.w700),
      calendarType: pickerType == PickerType.single
          ? CalendarDatePicker2Type.single
          : CalendarDatePicker2Type.range,
      selectedDayHighlightColor: WColors.primaryColor,
      closeDialogOnCancelTapped: true,
      firstDayOfWeek: 1,
      firstDate: firstDate,
      lastDate: lastDate,
      currentDate: currentDate,
      controlsTextStyle:  TextStyle(
        color: Colors.black,
        fontSize: 15.px,
        fontWeight: FontWeight.bold,
      ),
      centerAlignModePicker: true,
    //  customModePickerIcon: const SizedBox(),
      selectedDayTextStyle:
          const TextStyle(color: Colors.white, fontWeight: FontWeight.w700),
      yearBuilder: ({
        required year,
        decoration,
        isCurrentYear,
        isDisabled,
        isSelected,
        textStyle,
      }) {
        return Center(
          child: Container(
            decoration: decoration,
            height: 36.px,
            width: 72.px,
            child: Center(
              child: Semantics(
                selected: isSelected,
                button: true,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      year.toString(),
                      style: textStyle,
                    ),
                    if (isCurrentYear == true)
                      Container(
                        padding: EdgeInsets.all(2.px),
                        margin: const EdgeInsets.only(left: 5),
                        decoration: const BoxDecoration(
                          shape: BoxShape.circle,
                          color: Colors.redAccent,
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
